Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Getting Chunks Now let’s get back to the problem of retrieving data from the control’s buffer. This is done with the GetChunk method. The syntax is as follows:
    Inet1.GetChunk( size [,DataType] )
The size argument specifies how many bytes of data to retrieve; it is a type Long. The optional DataType argument specifies whether the data is text or binary. You use the same values for this argument as with the OpenURL method: icString (the default, value = 0) and icByteArray (value = 1). If you try to retrieve more bytes of data than are present in the buffer, you’ll simply get whatever is there. If you retrieve only part of the data in the buffer, the remainder of the data stays in the buffer ready to be retrieved with the next call to GetChunk. Remember, however, that we are dealing here with asynchronous data retrieval. This means that when you execute one of the data retrieval methods that places data in the Internet Transfer control’s buffer, program execution continues while the control waits for the data. How, then, do you know when the data has been received and is waiting in the buffer to be retrieved with GetChunk? The answer lies in the StateChanged event. Two of the possible events, as described earlier, can signal the successful receipt of data: icResponseReceived (value = 8) and icResponseCompleted (value = 12). The technique is to place the calls to GetChunk in the StateChanged event procedure, executing them if, and only if, one of these states has occurred. To allow for the possibility that more than one call to GetChunk will be necessary to retrieve all the data in the buffer, we can use a loop. Listing 18.2 shows the code, which displays the full buffer contents in a Rich Text Box control named RTB1. Listing 18.2 Using GetChunk in the StateChanged event procedure to retrieve data from the Internet Transfer Control buffer.
    Private Sub Inet1_StateChanged(ByVal State As Integer)

    Dim s1, s2

    Select Case State
        ‘ Other cases not shown.
        Case icResponseCompleted
        s1 = “”
        s2 = “”
            Do
                s1 = Inet1.GetChunk(512, icString)
                s2 = s2 & s1
             Loop Until s1 = “”
            RTB1.Text = s2
    End Select

    End Sub
The icResponseReceived state does not always mean that there is data in the buffer. Some operations, such as handshaking with an FTP site, result in this state without any data being placed in the buffer. If you wait for icResponseCompleted, you can be sure that any and all data has been received. The Execute Method Perhaps the most important method you’ll use with the Internet Transfer control is Execute. With this method, you can send a variety of commands to the remote computer. The syntax is:
    Inet1.Execute (url, operation, data, requestHeaders)
The url argument specifies the remote computer that the command will be sent to. This argument is optional; if it is omitted, the URL specified in the control’s URL property is used. The operation argument specifies the command to be sent, as explained later. The remaining two arguments are used only with certain commands. The commands that you can send with the Execute method depend on the connection protocol—FTP or HTTP. For an HTTP connection, four commands are available: • GET—Retrieves data from the server • HEAD—Retrieves the header only from the server • POST—Sends additional data required to complete a request • PUT—Uploads a file to the server Use of the POST and PUT commands is beyond the scope of this book. If you want to use these commands, you need to be fairly knowledgeable about the details of HTTP. When using the HEAD command, you will receive the HTTP headers, which are intercepted by the control and do not appear in the buffer. The most common use for the HEAD command is to verify that a URL is functioning without the overhead of retrieving the entire content with GET. Simply execute HEAD, and if the operation succeeds (as indicated by the “Response Completed” value being detected in the StateChanged event procedure), you know the URL is functional. The Internet Transfer control is most commonly used with GET. When using Execute with GET, the data returned by the server is placed in the Internet Transfer control’s internal buffer. You retrieve the data using the GetChunk method, as explained earlier. Here are some examples of using Execute with GET. To retrieve the entire default Microsoft home page:
    Inet.Execute “<http://www.microsoft.com>” , “GET”
To retrieve the “books” page from my Web site:
    Inet1.Execute “<http://www.pgacon.com/books.htm>”, “GET”
You can use the GET command to submit data to Common Gateway Interface (CGI) scripts—for example, submitting queries to Web search engines. You need to know the proper syntax, of course. For example, the following command searches the Yahoo site for references to “bananas.”
    Inet1.Execute “<http://search.yahoo.com/bin/search?p=banana>” , “GET”
Types Of Errors The many things that can go wrong when using the Internet Transfer control can be divided into two categories: • Certain situations generate an error in the control, resulting in the StateChanged event procedure being passed the icError value. Examples include trying to connect to a nonexistent Web site, or if your connection to the Internet is not functioning properly. • Other situations may not generate an error in the control, even though something is not working properly. For instance, if you request a nonexistent file from a functioning Web site, no error is generated; instead, the HTTP host returns an “Object Not Found” message. Even when a control error does not occur, your program needs to be on the alert for messages returned from the server indicating that something has gone wrong. An HTTP Demonstration The program presented in this section demonstrates using the Internet Transfer control for HTTP transfers. It is not intended to perform any task beyond helping you gain some familiarity with how the control works. The program is shown in Figure 18.1. The form contains a Rich Text Box control to display the information returned from the server, and a Text Box to display status messages as reported by the StatusChanged event procedure. Other elements include Text Boxes and Option Buttons to permit you to specify the URL and the command to be sent. Of course, an Internet Transfer control is on the form as well. I have enabled this program to use all four of the HTTP-related commands—GET, HEAD, PUT, and POST—so you can experiment with them. More details on the program’s objects and properties are presented in Listing 18.3. Figure 18.1 The Internet Transfer control demonstration program.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.